﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BussinessLogic;
using DataAccess;
using WSS.hameduconn.Bases;

namespace WSS.hameduconn
{
    public partial class Login : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                loginAlert.Visible = false;
        }

        protected void btLogin_Click(object sender, EventArgs e)
        {
            string userName = tbUsername.Text;
            string passWord = tbPassword.Text;
            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(passWord))
            {
                AlertError("Please input require fields");
                return;
            }
            BLUser userControl = new BLUser();
            BLAdmin adminControl = new BLAdmin();
            if (ddlRoles.SelectedValue == "1")
            {
                var adminLogin = adminControl.GetAdmin(userName.Trim(), new AUEncrypt().Encrypt(passWord));
                if (adminLogin != null)
                {
                    Session["OAuth"] = new Authenticate { admin = adminLogin, user = null };
                    Response.Redirect(ResolveUrl("~/Account.aspx"));
                }
                else
                {
                    AlertError("Login infomation incorrect");
                    return;
                }
            }
            else
            {
                var userLogin = userControl.GetUser(userName.Trim(), new AUEncrypt().Encrypt(passWord));
                if (userLogin != null)
                {
                    Session["OAuth"] = new Authenticate { admin = null, user = userLogin };
                    Response.Redirect(ResolveUrl("~/Profile.aspx"));
                }

                else
                {
                    AlertError("Login infomation incorrect");
                    return;
                }
            }

        }

        protected void AlertError(string msg)
        {
            loginAlert.Attributes["class"] = "alert alert-danger";
            loginAlert.InnerHtml = msg;
            loginAlert.Visible = true;
        }
    }
}